package com.company;
import java.util.HashMap;
import java.util.Scanner;

public class Game {
    private Room currentRoom;
    private HashMap<String,Handler> handlers = new HashMap<String, Handler>();

    public Game()
    {
        handlers.put("go",new HandlerGo(this));
        handlers.put("bye",new HandlerBye(this));
        handlers.put("help",new HandlerHelp(this));
        createRooms();
    }

    private void createRooms()
    {
        Room birth, normal1, normal2, littleboss1, littleboss2,altar,boss;

        //	制造房间
        birth = new Room("出生地");
        normal1 = new Room("普通关卡1");
        normal2 = new Room("普通关卡2");
        littleboss1 = new Room("蚌精窝");
        littleboss2 = new Room("龟丞相窝");
        altar = new Room("祭坛");
        boss = new Room("敖丙房间");

        //	初始化房间的出口
        birth.setExit("north",normal1);
        normal1.setExit("south",birth);
        normal1.setExit("north", littleboss1);
        normal1.setExit("east",normal2);
        normal2.setExit("west",normal1);
        normal2.setExit("north", littleboss2);
        normal2.setExit("northwest",altar);
        littleboss1.setExit("southeast",altar);
        littleboss1.setExit("east",littleboss2);
        littleboss1.setExit("south", normal1);

        littleboss2.setExit("west",littleboss1);
        littleboss2.setExit("south",normal2);

        altar.setExit("up",boss);
        currentRoom = birth;  //	从城堡门外开始
    }

    private void printWelcome() {
        System.out.println();
        System.out.println("欢迎来到城堡！");
        System.out.println("这是一个超级无聊的游戏。");
        System.out.println("如果需要帮助，请输入 'help' 。");
        System.out.println();
        showPrompt();
    }

    // 以下为用户命令

    public void goRoom(String direction)
    {
        Room nextRoom = currentRoom.getExit(direction);

        if (nextRoom == null) {
            System.out.println("那里没有门！");
        }
        else {
            currentRoom = nextRoom;
            showPrompt();
        }
    }

    public void showPrompt(){
        System.out.println("你在" + currentRoom);
        System.out.print("出口有: ");
        System.out.print(currentRoom.getExitDesc());
        System.out.println();
    }

    public void play(){
        Scanner in = new Scanner(System.in);

        while ( true ) {
            String line = in.nextLine();
            String[] words = line.split(" ");
            Handler handler = handlers.get(words[0]);
            String value = "";
            if(words.length > 1 )
                value =words[1];
            if( handler != null){
                handler.docmd(value);
                if(handler.isBye())
                    break;
            }
            
        }
        in.close();
    }
    public static void main(String[] args) {
        Game game = new Game();
        game.printWelcome();
        game.play();
        System.out.println("感谢您的光临。再见！");

    }

}
